﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Gmantis.Controls
{
    public class ScrollViewerContentPresenterClip : ScrollViewerCornerRadiusBase
    {
        // Fields
        private Geometry _output;
        public static readonly DependencyProperty ScrollContentPresenterProperty;

        // Methods
        static ScrollViewerContentPresenterClip()
        {
            ScrollContentPresenterProperty = DependencyProperty.Register("ScrollContentPresenter", typeof(ScrollContentPresenter), typeof(ScrollViewerContentPresenterClip), new PropertyMetadata(null, (d, a) => (d as ScrollViewerContentPresenterClip).UpdateContentPresenter(a.NewValue as ScrollContentPresenter)));
        }

        private void UpdateContentPresenter(ScrollContentPresenter scrollContentPresenter)
        {
            scrollContentPresenter.SizeChanged += (s, e) => this.UpdateOutput();
            this.UpdateOutput();
        }

        protected override void UpdateOutput()
        {
            if (((this.ScrollContentPresenter != null) && (this.ScrollContentPresenter.ActualWidth > 0.0)) && (this.ScrollContentPresenter.ActualHeight > 0.0))
            {
                CornerRadius cornerRadius = base.CornerRadius;
                if (base.CornerRadius != new CornerRadius())
                {
                    PathGeometry geometry = new PathGeometry();
                    PathFigure figure = new PathFigure
                    {
                        StartPoint = new Point(base.CornerRadius.TopLeft, 0.0)
                    };
                    double actualWidth = this.ScrollContentPresenter.ActualWidth;
                    double actualHeight = this.ScrollContentPresenter.ActualHeight;
                    CornerRadius radius = new CornerRadius(base.CornerRadius.TopLeft, (base.VerticalScrollBarVisibility == Visibility.Visible) ? 0.0 : base.CornerRadius.TopRight, ((base.VerticalScrollBarVisibility == Visibility.Visible) || (base.HorizontalScrollBarVisibility == Visibility.Visible)) ? 0.0 : base.CornerRadius.BottomRight, (base.HorizontalScrollBarVisibility == Visibility.Visible) ? 0.0 : base.CornerRadius.BottomLeft);
                    LineSegment segment = new LineSegment
                    {
                        Point = new Point(actualWidth - radius.TopRight, 0.0)
                    };
                    figure.Segments.Add(segment);
                    if (radius.TopRight > 0.0)
                    {
                        ArcSegment segment2 = new ArcSegment
                        {
                            SweepDirection = SweepDirection.Clockwise,
                            Size = new Size(radius.TopRight, radius.TopRight),
                            Point = new Point(actualWidth, radius.TopRight)
                        };
                        figure.Segments.Add(segment2);
                    }
                    LineSegment segment3 = new LineSegment
                    {
                        Point = new Point(actualWidth, actualHeight - radius.BottomRight)
                    };
                    figure.Segments.Add(segment3);
                    if (radius.BottomRight > 0.0)
                    {
                        ArcSegment segment4 = new ArcSegment
                        {
                            SweepDirection = SweepDirection.Clockwise,
                            Size = new Size(radius.BottomRight, radius.BottomRight),
                            Point = new Point(actualWidth - radius.BottomRight, actualHeight)
                        };
                        figure.Segments.Add(segment4);
                    }
                    LineSegment segment5 = new LineSegment
                    {
                        Point = new Point(radius.BottomLeft, actualHeight)
                    };
                    figure.Segments.Add(segment5);
                    if (radius.BottomLeft > 0.0)
                    {
                        ArcSegment segment6 = new ArcSegment
                        {
                            SweepDirection = SweepDirection.Clockwise,
                            Size = new Size(radius.BottomLeft, radius.BottomLeft),
                            Point = new Point(0.0, actualHeight - radius.BottomLeft)
                        };
                        figure.Segments.Add(segment6);
                    }
                    LineSegment segment7 = new LineSegment
                    {
                        Point = new Point(0.0, radius.TopLeft)
                    };
                    figure.Segments.Add(segment7);
                    if (radius.TopLeft > 0.0)
                    {
                        ArcSegment segment8 = new ArcSegment
                        {
                            SweepDirection = SweepDirection.Clockwise,
                            Size = new Size(radius.TopLeft, radius.TopLeft),
                            Point = new Point(radius.TopLeft, 0.0)
                        };
                        figure.Segments.Add(segment8);
                    }
                    geometry.Figures.Add(figure);
                    this._output = geometry;
                    base.RaisePropertyChanged("Output");
                }
            }
        }

        // Properties
        public Geometry Output
        {
            get
            {
                return this._output;
            }
        }

        public ScrollContentPresenter ScrollContentPresenter
        {
            get
            {
                return (ScrollContentPresenter)base.GetValue(ScrollContentPresenterProperty);
            }
            set
            {
                base.SetValue(ScrollContentPresenterProperty, value);
            }
        }
    }
}
